//1.顶部的下拉菜单
//2。轮播图
$(function() {
	$('.shop').hover(function() {
		$('#drop-down').stop().slideDown()
	}, function() {
		$('#drop-down').stop().slideUp()
	})

	$('.site-list>ul>li').hover(function() {
		//alert(this.id+'-1')
		var id = this.id + '-1';
		//alert(id)
		$('#' + id).stop().show();
		//$('#submenu1-1').stop().slideDown();
	}, function() {
		var id = this.id + '-1';
		$('#' + id).stop().hide()
		//$('#submenu1-1').stop().slideUp()
	})

	$('.category-list>ul>li').hover(function() {
		var $id = this.id + "-1";
		$('#' + $id).stop().show();
	}, function() {
		var $id = this.id + "-1";
		$('#' + $id).stop().hide();
	})

	//提示框

	/*$('.site-search').click(function() {
		$('.search-word').stop().hide();
		$('.prompt').stop().show();
	})
	$('.site-search').mouseleave(function() {
		$('.search-word').stop().show();
		$('.prompt').stop().hide();
	})*/
	$('.search-text').on('keyup focus', function() {
		$('.search-word').hide();
		$('.prompt').show();
	}).blur(function() {
		$('.search-word').show();
		$('.prompt').hide();
	})

	//滚动条到100px的时候，显示回到顶部，
	//兼容
	/*var scroll = $(document.documentElement).scrollTop()+$(document.body).scrollTop()
	console.log(scroll)
	$(window).scroll(function(){
		
		if(scroll>100){
		console.log(scroll)
		$('.fixed2').css('display','block')
	}
	})*/

	$(window).scroll(function() {
		var top = $(window).scrollTop();
		if(top > 600) {
			$('.fixed2').css('display', 'block');
		} else {
			$('.fixed2').css('display', 'none');
		}

	})

	//点击回到顶部，滚动条回到顶部

	$('.fixed2').click(function() {
		$('html,body').scrollTop(0)
	})

	//计时器
	//console.log($('#id-H').html())//jqery获取span的值

	setInterval(function() {
		var xzdata = new Date;
		var wldata = new Date("2020-12-26 14:00");
		var d = h = m = s = 0;

		var jcs = parseInt((wldata - xzdata) / 1000);

		d = parseInt(jcs / 86400);
		h = parseInt(jcs / 3600 % 24);
		m = parseInt(jcs / 60 % 60);
		s = parseInt(jcs % 60);

		function time(num) {
			if(num < 10) {
				return "0" + num;
			} else {
				return num
			}
		}
		$('#hours').html(time(h));
		$('#min').html(time(m));
		$('#seconds').html(time(s))
	}, 1000)

	/*轮播开始*/
	var prevIndex, nextIndex;
	var len;
	var id;
	init();

	function init() {
		prevIndex = nextIndex = 0;
		len = document.querySelectorAll('.list .item').length; //轮播的长度

		document.querySelector('.prev').onclick = function() {
			slidePrev();
		}

		document.querySelector('.next').onclick = function() {
			slideNext();
		}

		var bullets = document.querySelectorAll('.site-slider .slider-item .bullet');
		for(var i = 0; i < bullets.length; i++) {
			bullets[i].index = i;
			bullets[i].onclick = function() {
				prevIndex = nextIndex;
				nextIndex = this.index;
				slideTo(prevIndex, nextIndex);
			}
		}

		var slider = document.querySelector('.site-slider');
		slider.onmouseover = function() {
			stop();
		}

		slider.onmouseout = function() {
			auto();
		}

		auto();
	}

	function slidePrev() {
		prevIndex = nextIndex;
		nextIndex--;
		if(nextIndex === -1) {
			nextIndex = len - 1;
		}
		slideTo(prevIndex, nextIndex);
	}

	function slideNext() {
		prevIndex = nextIndex;
		nextIndex++;
		if(nextIndex === len) {
			nextIndex = 0;
		}
		slideTo(prevIndex, nextIndex);
	}

	function slideTo(prev, next) {

		var lis = document.querySelectorAll('.list .item');
		var bullets = document.querySelectorAll('.site-slider .slider-item .bullet');

		bullets[prev].className = 'bullet';
		bullets[next].className = 'bullet focus';

		animate(lis[prev], {
			opacity: 0
		});
		animate(lis[next], {
			opacity: 100
		});
	}

	function auto() {
		clearInterval(id);
		id = setInterval(function() {
			slideNext();
		}, 2000)
	}

	function stop() {
		clearInterval(id);
	}

	function getStyle(el, property) {
		if(getComputedStyle) { //兼容问题
			return getComputedStyle(el)[property]
		} else {
			return el.currentStyle[property];
		}
	}

	function animate(el, properties) {

		clearInterval(el.timerId);

		el.timerId = setInterval(function() {

			for(var property in properties) {
				var current;
				var target = properties[property];

				if(property === 'opacity') {
					current = Math.round(parseFloat(getStyle(el, 'opacity')) * 100);
				} else {
					current = parseInt(getStyle(el, property));

				}
				var speed = (target - current) / 30;
				speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
				if(property === 'opacity') {
					el.style.opacity = (current + speed) / 100;
				} else {
					el.style[property] = current + speed + 'px';
				}
			}
		}, 20)
	}

	/*轮播 结束*/

})